﻿using System;
using System.Collections.Generic;
using Cesave.Domain.PermisosModule.Entities;
using Cesave.Domain.PermisosModule.Contracts.Agentes;
using Cesave.Infrastructure.CrossCutting.IoC;
using Cesave.Infrastructure.CrossCutting.Logging;
using Cesave.DistributedServices.Core.ErrorHandlers;
using Cesave.DistributedServices.PermisosModule.Resources;
using System.ServiceModel;

namespace Cesave.DistributedServices.PermisosModule
{
	public partial class PermisosModuleService
	{
		#region IPermisosModuleService Members

		/// <summary>
		/// 
		/// </summary>
		/// <param name="juntaGuid"></param>
		/// <returns></returns>
		public List<Agente> FindAgentesByJunta(Guid juntaGuid)
		{
			try
			{
				IAgenteService agenteService = IoCFactory.Resolve<IAgenteService>();
				return agenteService.FindAgentesByJunta(juntaGuid);
			}
			catch (InvalidOperationException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
			catch (ArgumentNullException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="juntaGuid"></param>
		/// <param name="status"></param>
		/// <returns></returns>
		public List<Agente> FindAgentesByJuntaAndStatus(Guid juntaGuid, bool status = true)
		{
			try
			{
				IAgenteService agenteService = IoCFactory.Resolve<IAgenteService>();
				return agenteService.FindAgentesByJuntaAndStatus(juntaGuid, status);
			}
			catch (InvalidOperationException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
			catch (ArgumentNullException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="aclGuid"></param>
		/// <returns></returns>
		public List<Agente> FindAgentesByAcl(Guid aclGuid)
		{
			try
			{
				IAgenteService agenteService = IoCFactory.Resolve<IAgenteService>();
				return agenteService.FindAgentesByAcl(aclGuid);
			}
			catch (InvalidOperationException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
			catch (ArgumentNullException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="agenteGuid"></param>
		/// <returns></returns>
		public Agente FindAgenteByGuid(Guid agenteGuid)
		{
			try
			{
				IAgenteService agenteService = IoCFactory.Resolve<IAgenteService>();
				return agenteService.FindAgenteByGuid(agenteGuid);
			}
			catch (InvalidOperationException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
			catch (ArgumentNullException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="agente"></param>
		public void AddAgente(Agente agente)
		{
			try
			{
				IAgenteService agenteService = IoCFactory.Resolve<IAgenteService>();
				agenteService.AddAgente(agente);
			}
			catch (ArgumentNullException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="agente"></param>
		public void ChangeAgente(Agente agente)
		{
			try
			{
				IAgenteService agenteService = IoCFactory.Resolve<IAgenteService>();
				agenteService.ChangeAgente(agente);
			}
			catch (ArgumentNullException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="agente"></param>
		public void DeleteAgente(Agente agente)
		{
			try
			{
				IAgenteService agenteService = IoCFactory.Resolve<IAgenteService>();
				agenteService.AddAgente(agente);
			}
			catch (ArgumentNullException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="agente"></param>
		/// <param name="oldPassword"></param>
		/// <param name="newPassword"></param>
		public void ChangePassword(Agente agente, string oldPassword, string newPassword)
		{
			try
			{
				IAgenteService agenteService = IoCFactory.Resolve<IAgenteService>();
				agenteService.AddAgente(agente);
			}
			catch (InvalidOperationException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
			catch (ArgumentNullException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="userName"></param>
		/// <returns></returns>
		public Agente FindAgenteByUsuario(string userName)
		{
			try
			{
				IAgenteService agenteService = IoCFactory.Resolve<IAgenteService>();
				return agenteService.FindAgenteByUsuario(userName);
			}
			catch (InvalidOperationException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
			catch (ArgumentNullException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		#endregion
	}
}
